<?php
class goods extends shopAdminBase {
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("商品管理");
        $this->cms->setControlFile("shop/goods/goods_control.json");

    }

    function indexTask($page,$id,$shopName,$goodsLabel,$orderBy,$asc){
        $goods=GoodsMode::init();
        $this->cms->setActionTitle("所有商品");
        $page=empty($page)||$page<0 ? 1:(int)$page;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $shopName=isset($shopName)?trim($shopName):null;
        $asc = $asc==1?true:false;
        $r['id']=$id;
        $r['shopName']=$shopName;
        $r['goodsLabel']=$goodsLabel;
        if(!empty($orderBy)){
            $r[$orderBy]=$asc;
        }
        if(isset($id)){
            $r['goods_info'][0]=$goods->getDetailInfo($id);
            $total=!empty($r['goods_info'])?1:0;
        }else{
            list($r['goods'],$total)=$goods->getList($this->shopInfo['id'],$goodsLabel,null,$shopName,$orderBy,$asc,$countOfPage,$offset);
            if($total!=0 && !empty($r['goods'])){
                $r['goods_info']=GoodsMode::init()->getDetailInfo($r['goods']);
                GoodsMode::init()->sortGoods($r['goods_info'],"create_time",false);
                if(isset($asc)){
                    GoodsMode::init()->sortGoods($r['goods_info'],$orderBy,$asc);
                }
            }
        }
        list($r['label'],$label_total)=LabelModule::init()->getList(30,0);
        usort($r['label'],function($a,$b){
            return ord(substr(iconv('utf-8', 'gbk', $a['name']), 0, 1))-
            ord(substr(iconv('utf-8', 'gbk', $b['name']), 0, 1));
        });
        $r['page']=new Page($page,$total,$countOfPage);
        $url=WebRouter::init()->getQuestion("id={$id}&shopName={$shopName}&goodsLabel={$goodsLabel}&orderBy={$orderBy}&asc={$asc}&page=");
        $r['page']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $r['shopType']=array("normal"=>"普通商品","commodity"=>"大宗商品","service"=>"服务商品","non_sale"=>"广告商品",);
        $this->cms->tableScene($r,"shop/goods/goods.php");
    }

    function detailTask($gopen_id){
        $this->cms->setActionTitle("修改商品");
        $goods=GoodsMode::init();
        $cats=MenuModule::init();

        $data=$goods->getDetailInfo($gopen_id);// gopen id
        if(empty($data)|| $data['shop_id']!=$this->shopInfo['id']){
            $r['message']="未找到该商品";
            View::displayAsHtml($r,"plugin/state/tips.php");
        }

        $r=$data;
        $r['cat']=$cats->getDetail($data['goods_cat_id']);
        $this->cms->formScene($r,'shop/goods/goodsModify.php');
    }
}